package com.aquarium.ui.controller;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aquarium.R;
import com.aquarium.module.ModuleController;
import com.aquarium.ui.ConnectDialogActivity;

public class ModuleSwitchableLayout extends LinearLayout {

	protected final static int REQUEST_CONNECT_WATER_MOTOR = 1;
	protected final static int REQUEST_CONNECT_RPOTEIN_SKIMMER = 2;
	protected final static int CONNECT_SECONDS = 5;

	protected Activity mActivity;
	protected Handler mHandler;

	protected String mModuleName;

	protected View mView;
	protected TextView mModuleTextView;
	protected RelativeLayout mSettingLayout;
	protected Switch mSwitch;
	protected RadioButton mOpenRadioButton;
	protected RadioButton mCloseRadioButton;
	protected Button mDoNowButton;
	protected Button mSettingButton;

	protected ModuleController mModuleController;

	private DoNowButtonListenerBinder mDoNowButtonListenerBinder;

	public ModuleSwitchableLayout(Activity activity, Handler handler,
			String moduleName, Class settingClass, int intentRqstCode) {
		super(activity);
		mActivity = activity;
		mHandler = handler;
		mModuleName = moduleName;
		mModuleController = ModuleController.getInstance(activity);

		LayoutParams params = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

		LinearLayout layout = new LinearLayout(activity);
		layout.setLayoutParams(params);

		mView = View.inflate(activity, R.layout.module_ctrl_type_1, this);
		initLayout(settingClass, intentRqstCode);
	}

	public void initLayout(Class settingClass, int intentRqstCode) {
		findViews();
		initViews();
		setSwitcherListener();
		setDoNowButtonListener(intentRqstCode);
		setSettingButtonListener(settingClass);
	}

	public boolean isOpenRadioButtonChecked() {
		return mOpenRadioButton.isChecked();
	}

	public void switchRadioButton() {
		boolean isChecked = isOpenRadioButtonChecked();
		if (isChecked) {
			mCloseRadioButton.setChecked(true);
		} else {
			mOpenRadioButton.setChecked(false);
		}
	}

	public void setRadioButtonOpen(boolean isOpen) {
		if (isOpen) {
			mOpenRadioButton.setChecked(true);
			mCloseRadioButton.setChecked(false);
		} else {
			mOpenRadioButton.setChecked(false);
			mCloseRadioButton.setChecked(true);
		}
	}

	protected void findViews() {
		mModuleTextView = (TextView) mView.findViewById(R.id.module_name);

		mSettingLayout = (RelativeLayout) mView
				.findViewById(R.id.manu_setting_layout);
		mSwitch = (Switch) mView.findViewById(R.id.switch_btn);
		mOpenRadioButton = (RadioButton) mView
				.findViewById(R.id.open_radio_btn);
		mCloseRadioButton = (RadioButton) mView
				.findViewById(R.id.close_radio_btn);
		mDoNowButton = (Button) mView.findViewById(R.id.do_now_btn);
		mSettingButton = (Button) mView.findViewById(R.id.time_setting_btn);
	}

	protected void initViews() {
		String mode = mModuleController.getWaterMotorMode();
		if (ModuleController.PREFER_VALUE_MODULE_AUTO.equals(mode)) {
			mSwitch.setChecked(true);
		} else if (ModuleController.PREFER_VALUE_MODULE_MANU.equals(mode)) {
			mSwitch.setChecked(false);
		}
		boolean isOn = mModuleController.isWaterMotorOn();
		if (isOn) {
			mOpenRadioButton.setChecked(true);
		} else {
			mCloseRadioButton.setChecked(true);
		}
		if (mSwitch.isChecked()) {
			mSettingButton.setVisibility(View.VISIBLE);
			mSettingLayout.setVisibility(View.GONE);

		} else {
			mSettingButton.setVisibility(View.GONE);
			mSettingLayout.setVisibility(View.VISIBLE);
		}
		mModuleTextView.setText(mModuleName);
	}

	public void setSwitcher(boolean isOn) {
		if (isOn) {
			mSwitch.setChecked(true);
		} else {
			mSwitch.setChecked(false);
		}
	}

	protected void setSwitcherListener() {
		mSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {

				if (isChecked) {
					mSettingButton.setVisibility(View.VISIBLE);
					mSettingLayout.setVisibility(View.GONE);
					mModuleController.setModuleMode(mModuleName,
							ModuleController.PREFER_VALUE_MODULE_AUTO);
				} else {
					mSettingButton.setVisibility(View.GONE);
					mSettingLayout.setVisibility(View.VISIBLE);
					mModuleController.setModuleMode(mModuleName,
							ModuleController.PREFER_VALUE_MODULE_MANU);
				}
			}
		});
	}

	protected void setDoNowButtonListener(final int intentRqstCode) {
		mDoNowButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				Intent intent = new Intent();
				intent.putExtra(ConnectDialogActivity.KEY_CONNECT_SECONDS,
						CONNECT_SECONDS);
				intent.setClass(mActivity, ConnectDialogActivity.class);
				mActivity.startActivityForResult(intent, intentRqstCode);

				if (mDoNowButtonListenerBinder != null) {
					boolean result = mDoNowButtonListenerBinder.binder(
							mModuleName, mOpenRadioButton, mCloseRadioButton);
					if (result) {
						return;
					}
				}

				if (mOpenRadioButton.isChecked()) {
					mModuleController.sendModuleOnOffCommand(mModuleName, true,
							true, mHandler);
				} else if (mCloseRadioButton.isChecked()) {
					mModuleController.sendModuleOnOffCommand(mModuleName,
							false, true, mHandler);
				}
			}
		});
	}

	protected void setSettingButtonListener(final Class cls) {
		if (mSettingButton != null) {
			mSettingButton.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View view) {
					Intent intent = new Intent();
					intent.setClass(mActivity, cls);
					intent.putExtra(ControllerSetting.INTENT_DATA_TITLE,
							mModuleName);
					mActivity.startActivity(intent);
				}
			});
		}
	}

	public void setDoNowButtonListenerBinder(DoNowButtonListenerBinder binder) {
		mDoNowButtonListenerBinder = binder;
	}

	public interface DoNowButtonListenerBinder {
		public boolean binder(String moduleName, RadioButton openRadioBtn,
				RadioButton closeRadioBtn);
	}
}
